package com.example.student_management.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.student_management.entity.Student;

// MyBatis 映射接口（数据库操作）
@Mapper // 标记为 MyBatis 映射接口（Spring 自动扫描）
public interface StudentMapper{
    
    // 查询所有学生
    @Select("SELECT * FROM student ORDER BY create_time DESC")
    List<Student> selectAll();

    // 根据id查询学生
    @Select("SELECT * FROM student WHERE id = #{id}")
    Student selectById(Integer id);

    // 根据keyword查询学生
    @Select("SELECT * FROM student WHERE name LIKE CONCAT('%', #{keyword}, '%') OR student_id LIKE CONCAT('%', #{keyword}, '%') ORDER BY create_time DESC")
    List<Student> selectByKeyword(String keyword);

//     // 新增学生
    @Insert("INSERT INTO student (student_id, name, age, gender) " +
            "VALUES (#{studentId}, #{name}, #{age}, #{gender})")
    int insert(Student student); // 返回受影响行数

    // 修改学生
    @Update("UPDATE student SET student_id = #{studentId}, name = #{name}, " +
            "age = #{age}, gender = #{gender} WHERE id = #{id}")
    int update(Student student);

    // 删除学生
    @Delete("DELETE FROM student WHERE id = #{id}")
    int delete(Integer id);
}
